<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title>Grade Bay</title>
		<link rel="stylesheet" href="gui.css" type="text/css" />
		<!--[if IE 7]>
			<link rel="stylesheet" href="css/ie7.css" type="text/css" />
		<![endif]-->
	</head>
	<body>
		<div class="page">
			<div class="header">
				<a href="index.php" id="logo"><img src="images/gradeBaylogo.png" width="330" height="79" alt=""/></a>
				<ul>
					<li class="selected"><a href="">EDIT SUBJECT</a></li>
					
				
				</ul>
			</div>
			<div class="body">
                          
<?php
     include 'PHPClasses/DBAccess.php';
     include 'PHPClasses/SubjectDBAccess.php';
     include 'PHPClasses/Subject.php';
     include 'PHPClasses/GradeCriterion.php';
                                 
     error_reporting(E_ERROR | E_PARSE);
                                    
     error_reporting (E_ALL ^ E_NOTICE);
                                    
                                    
     if (isset($_POST['submitted']))
     { 
                                    $DBAccess=new SubjectDBAccess();
                                    $Subject=new Subject();
                                    $GradeCriteria1st=new ArrayObject();
                                    $GradeCriteria2nd=new ArrayObject();
                                    $GradeCriteria3rd=new ArrayObject();
                                    $GradeCriteria4th=new ArrayObject();
                                    
                                    $subjectID=$_POST['subjectID'];
                                    $subjectName=$_POST["subjName"];
                                    $passingGrade=$_POST["passing"]; 
                                    $level=$_POST["level"];
                                    $idTeacher=$_POST["teacher"];
                                      
                                    $Subject->setIdSubject($subjectID);
                                    $Subject->setSubjectName($subjectName);
                                    $Subject->setPassingGrade($passingGrade);
                                    $Subject->setSubjectLevel($level);
                                    $Subject->setTeacherInCharge($idTeacher);
                                    
                                    $OverOrUnder100=FALSE;
                                 
                                    $criteriaID=$_POST["criteriaID"];
                                    $criteria1ID=$_POST["criteria1ID"];
                                    $criteria2ID=$_POST["criteria2ID"];
                                    $criteria3ID=$_POST["criteria3ID"];
                                    
                                   
                                    
                                    $c=$_POST["criteria"];
                                    $cp=$_POST["critPercent"];
                                    $c1=$_POST["criteria1"];
                                    $cp1=$_POST["critPercent1"];
                                    $c2=$_POST["criteria2"];
                                    $cp2=$_POST["critPercent2"];
                                    $c3=$_POST["criteria3"];
                                    $cp3=$_POST["critPercent3"];
                                    $nc=$_POST["newcriteria"];
                                    $np=$_POST["newcritPercent"];
                                    $n1=$_POST["newcriteria1"];
                                    $np1=$_POST["newcritPercent1"];
                                    $n2=$_POST["newcriteria2"];
                                    $np2=$_POST["newcritPercent2"];
                                    $n3=$_POST["newcriteria3"];
                                    $np3=$_POST["newcritPercent3"];
                                    
                                    
                            
                           
                                 
                              //update         
                               for($i=0;$i<sizeof($c);$i++){
                                            if($c[$i]==""||$cp[$i]==""){
                                                $GradeCriteria1st=null;
                                            }
 
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($c[$i]);
                                            $GradeCriterion->setIdGradeCriterion($criteriaID[$i]);
                                            $GradeCriterion->setPercentage($cp[$i]);
                                            $GradeCriterion->setQuarter("1st Quarter");
                                            $GradeCriteria1st->append($GradeCriterion);
                                            }
                               }
                               
                               for($i=0;$i<sizeof($c1);$i++){


                                            if($c1[$i]==""||$cp1[$i]==""){
                                                $GradeCriteria2nd=null;
                                            }
                                            
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($c1[$i]);
                                            $GradeCriterion->setIdGradeCriterion($criteria1ID[$i]);
                                            $GradeCriterion->setPercentage($cp1[$i]);
                                            $GradeCriterion->setQuarter("2nd Quarter");
                                            $GradeCriteria2nd->append($GradeCriterion);
                                            }
                               }
                               
                               
                               for($i=0;$i<sizeof($c2);$i++){


                                            if($c2[$i]==""||$cp2[$i]==""){
                                                $GradeCriteria3rd=null;
                                            }
                                            
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($c2[$i]);
                                            $GradeCriterion->setIdGradeCriterion($criteria2ID[$i]);
                                            $GradeCriterion->setPercentage($cp2[$i]);
                                            $GradeCriterion->setQuarter("3rd Quarter");
                                            $GradeCriteria3rd->append($GradeCriterion);
                                            }


                               }
                               
                               
                                  for($i=0;$i<sizeof($c3);$i++){


                                            if($c3[$i]==""||$cp3[$i]==""){
                                                $GradeCriteria4th=null;
                                            }
                                            
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($c3[$i]);
                                            $GradeCriterion->setIdGradeCriterion($criteria3ID[$i]);
                                            $GradeCriterion->setPercentage($cp3[$i]);
                                            $GradeCriterion->setQuarter("4th Quarter");
                                            $GradeCriteria4th->append($GradeCriterion);
                                            }


                               }
                               
                               
                             if($np){
                                 $cp = array_map("array_sum", array_map(null, $np, $cp)); 
                             }
                             if($np1){
                                $cp1= array_map("array_sum", array_map(null, $np1, $cp1));
                             }
                             
                             if($np2){
                                $cp2= array_map("array_sum", array_map(null, $np2, $cp2));
                             }
                             
                             if($np3){
                                $cp3= array_map("array_sum", array_map(null, $np3, $cp3));
                             }
                                     
                             
                             
                              if(array_sum($cp)<100||array_sum($cp)>100||array_sum($cp1)<100||array_sum($cp1)>100||array_sum($cp2)<100||array_sum($cp2)>100
                                            ||array_sum($cp3)<100||array_sum($cp3)>100){
                                $OverOrUnder100=TRUE;
                            }
        
                          
                        //add
                               
                                  $DBAccess->setIdSubjectGenerated($Subject->getIdSubject());
                                  $newGradeCriteria1st=new ArrayObject();
                                  $newGradeCriteria2nd=new ArrayObject();
                                  $newGradeCriteria3rd=new ArrayObject();
                                  $newGradeCriteria4th=new ArrayObject();
                                    
                                  
                                  
                                 
                                  for($i=0;$i<sizeof($nc);$i++){


                                            if($nc[$i]==""||$np[$i]==""){
                                                $newGradeCriteria1st=null;
                                            }
                                            
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($nc[$i]);
                                            $GradeCriterion->setPercentage($np[$i]);
                                            $GradeCriterion->setQuarter("1st Quarter");
                                             $newGradeCriteria1st->append($GradeCriterion);
                                            }


                                
                                 }
                                  
                                
                                for($i=0;$i<sizeof($n1);$i++){


                                            if($n1[$i]==""||$np1[$i]==""){
                                                 $newGradeCriteria2nd=null;
                                            }
                                            
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($n1[$i]);
                                            $GradeCriterion->setPercentage($np1[$i]);
                                            $GradeCriterion->setQuarter("2nd Quarter");
                                            
                                            
                                        
                                             $newGradeCriteria2nd->append($GradeCriterion);
                                            
                                            }


                                }
                                 
                                 
                                 
                                  
                                  for($i=0;$i<sizeof($n2);$i++){


                                            if($n2[$i]==""||$np2[$i]==""){
                                                $newGradeCriteria3rd=null;
                                            }
                                            
                                         
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($n2[$i]);
                                            $GradeCriterion->setPercentage($np2[$i]);
                                            $GradeCriterion->setQuarter("3rd Quarter");
                                            
                                           
                                            
                                            $newGradeCriteria3rd->append($GradeCriterion);
                                            }


                                }
                                    
                                
                                   
                                    
                                  for($i=0;$i<sizeof($n3);$i++){


                                            if($n3[$i]==""||$np3[$i]==""){
                                                 $newGradeCriteria4th=null;
                                            }
                                            
                                            else{
                                            $GradeCriterion=new GradeCriterion();
                                            $GradeCriterion->setName($n3[$i]);
                                            $GradeCriterion->setPercentage($np3[$i]);
                                            $GradeCriterion->setQuarter("4th Quarter");
                                            
                                            
                                            
                                           $newGradeCriteria4th->append($GradeCriterion);
                                            }


                                }
                                
                                       


                                    
                                  
                                  
                                  
                                   
                                 $errormsg=$DBAccess->validate($Subject,$GradeCriteria1st,$GradeCriteria2nd,$GradeCriteria3rd,$GradeCriteria4th,TRUE,$OverOrUnder100);
                                
                                 
                                 if(!$errormsg){
                                    $errormsg=$DBAccess->validate($Subject,$newGradeCriteria1st,$newGradeCriteria2nd,$newGradeCriteria3rd,$newGradeCriteria4th,TRUE,FALSE); 
                                 }
                                  
                                           
                                 if ($errormsg){ //If any errors display them  
                                     header("Location: http://localhost/paragox-infinity-introse/editSubject.php?error=".$errormsg."&subjectID=".$subjectID);
                                  }
                                  
                                 else{
                                      
                                      
                                   $DBAccess->setIdSubjectGenerated($Subject->getIdSubject());
                                   $DBAccess->updateDB($Subject);
                                     
                                   $DBAccess->updateCriterionDB($GradeCriteria1st);
                                   $DBAccess->updateCriterionDB($GradeCriteria2nd);
                                   $DBAccess->updateCriterionDB($GradeCriteria3rd);
                                   $DBAccess->updateCriterionDB($GradeCriteria4th);
                                   
                                
                                  if(!empty($newGradeCriteria1st)&&!empty($newGradeCriteria2nd)&&!empty($newGradeCriteria3rd)&&!empty($newGradeCriteria4th))
                                  {
                                    
                                   $DBAccess->addCriterionToDB($newGradeCriteria1st);
                                   $DBAccess->addCriterionToDB($newGradeCriteria2nd);
                                   $DBAccess->addCriterionToDB($newGradeCriteria3rd);
                                   $DBAccess->addCriterionToDB($newGradeCriteria4th);
                                  
                                 }
                                  
                                  
                                  
                                   
                                   
                                  echo "<script> 
                                if(confirm('Subject updated! Forgot Something?')){
                                        window.location='http://localhost/paragox-infinity-introse/editSubject.php?subjectID=".$subjectID."';
                                }
                                else{
                                    window.location='http://localhost/paragox-infinity-introse/viewSubjects.php';
                                    }   

                                </script>"; 
                               }
                                 
                                  
                               
                                  $DBAccess->DBDisconnect(); 
 }
                                    

 ?>
                             
				
	                            		
			</div>
			
		</div>
	</body>
</html>  